<?php

class Webkul_Marketplacepartner_Block_Products_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct(){
        parent::__construct();
        $this->setId('customersProducts');
        $this->setUseAjax(true);
        //$this->setDefaultSort('mageproductid');
        $this->setDefaultDir('DESC');
        $this->_emptyText = Mage::helper('marketplacepartner')->__('No Products Found.');
    }
	
	 protected function _prepareCollection(){
	   $mysqlprefix = Mage::getConfig()->getTablePrefix();//code for mysql prefix
        $mytablepartnerstatus=$mysqlprefix.'marketplacepartner_entity_userdata';
        $mytabledata=$mysqlprefix.'marketplacepartner_entity_data';
        $customerModel = Mage::getModel('customer/customer')->getCollection();
            $collection = Mage::getResourceModel('marketplacepartner/userdata_collection');
        if($this->getRequest()->getParam('unapp')==1){
           $collection->addFieldToFilter('status', array('neq' => '1'));

        }
			$this->setCollection($collection);
			parent::_prepareCollection();
			$customerModel = Mage::getModel('customer/customer');
			
		//Modify loaded collection
        foreach ($this->getCollection() as $item) {
		$customer = $customerModel->load($item->getuserid());
            $item->customer_name = sprintf('<a href="%s" title="View Customer\'s Profile">%s</a>',
            								$this->getUrl('adminhtml/customer/edit/id/' . $item->getuserid()),
            								$customer->getName()
            							  );
										  
		$item->prev = sprintf('<span data="%s" product-id="%s" customer-id="%s" title="Click to Review" class="prev btn">prev</span>',$this->getUrl('marketplacepartner/prev/index/id/' .$item->getMageproductid()),$item->getProductId(),$item->getCustomerId());
		   $item->entity_id = (int)$item->getmageproductid();
             if(!(is_null($item->getmageproductid())) && $item->getmageproductid() != 0){
			 if($item->getstatus() == 1){
				$item->status = sprintf('<a href="%s" title="View product">Approved</a>',
										 $this->getUrl('adminhtml/catalog_product/edit/id/' . $item->getmageproductid())
									    );
				}
				else
				{
				 $item->status = sprintf('<a href="%s" title="Click to Approve" onclick="return confirm(\'You sure?\')">Unapproved</a>',$this->getUrl('marketplacepartner/products/approve/id/' . $item->getmageproductid()));
				}
				
				$product = Mage::getModel('catalog/product')->load($item->getmageproductid());
				$stock_inventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($item->getmageproductid());
				$item->name = $product->getName();
				$item->weight = $product->getWeight();
				$item->price = $product->getPrice();
				$item->stock = $stock_inventory->getQty();
				
		
			$qtySold = Mage::getModel('marketplacepartner/userdata')->quantitySold($item->getmageproductid());
			$item->qty_sold = (int)$qtySold;
			$amountEarned = Mage::getModel('marketplacepartner/userdata')->amountEarned($item->getmageproductid());
			$item->amount_earned = $amountEarned;
			$cleared_act = Mage::getModel('marketplacepartner/userdata')->clearedAt($item->getmageproductid());
			foreach($cleared_act as $clear){
			if ( isset($clear) && $clear != '0000-00-00 00:00:00' ) {$item->cleared_at = $clear;}
			}
			$created_at = Mage::getModel('marketplacepartner/userdata')->createdAt($item->getmageproductid());
			foreach($created_at as $clear1){
			if ( isset($clear1) && $clear1 != '0000-00-00 00:00:00' ) {$item->created_at = $clear1;}
			}
            }
		}
	    $this->setCollection($collection);
		return parent::_prepareCollection();
			
	 }
	 
	
	  protected function _prepareColumns(){
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('marketplacepartner')->__('ID'),
            'width'     => '50px',
            'index'     => 'entity_id',
            'type'  => 'number',
			'filter'    => false,
            'sortable'  => false
        ));
	   
        $this->addColumn('customer_name', array(
            'header'    => Mage::helper('marketplacepartner')->__('Customer Name'),
            'index'     => 'customer_name',
            'type'  => 'text',
			'filter'    => false,
            'sortable'  => false
        ));
      
	   $this->addColumn('name', array(
            'header'    => Mage::helper('marketplacepartner')->__('Name'),
            'index'     => 'name',
            'type'  => 'string',
			'filter'    => false,
            'sortable'  => false
        ));
		 $this->addColumn('price', array(
            'header'    => Mage::helper('marketplacepartner')->__('Price'),
            'index'     => 'price',
            'type'  => 'price',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('stock', array(
            'header'    => Mage::helper('marketplacepartner')->__('Stock'),
            'index'     => 'stock',
            'type'  => 'number',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('weight', array(
            'header'    => Mage::helper('marketplacepartner')->__('Weight'),
            'index'     => 'weight',
            'type'  => 'number',
			'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('status', array(
            'header'    => Mage::helper('marketplacepartner')->__('Status'),
            'index'     => 'status',
            'type'  => 'text',
            'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('prev', array(
            'header'    => Mage::helper('marketplacepartner')->__('Preview'),
            'index'     => 'prev',
            'type'  => 'text',
            'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('qty_sold', array(
            'header'    => Mage::helper('marketplacepartner')->__('Qty. Sold'),
            'index'     => 'qty_sold',
            'type'  => 'number',
            'filter'    => false,
            'sortable'  => false
        ));
        $this->addColumn('amount_earned', array(
            'header'    => Mage::helper('marketplacepartner')->__('Earned'),
            'index'     => 'amount_earned',
            'type'  => 'price',
            'filter'    => false,
            'sortable'  => false
        ));
        
        $this->addColumn('created_at', array(
            'header'    => Mage::helper('marketplacepartner')->__('Created'),
            'index'     => 'created_at',
            'type'  => 'datetime',
			'filter'    => false,
            'sortable'  => false
        ));
        return parent::_prepareColumns();
		}
}
